<template>
  <el-dialog title="参数表" :visible.sync="visible" width="800px" top="5vh" append-to-body>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
      <el-form-item label="参数名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入参数名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['prod:parameter:add']"
        >新增</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="parameterList" height="260px">
      <el-table-column label="参数名称" align="center" prop="name" />
      <el-table-column label="参数值" align="center">
        <template slot-scope="scope">
          <span>{{concat(scope.row.parameterValueList)}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['prod:parameter:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['prod:parameter:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改参数名对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="参数名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入参数名称" />
        </el-form-item>
        <el-divider content-position="center">参数值信息</el-divider>

        <el-tag
          :key="tag.value"
          v-for="tag in parameterValueList"
          closable
          :disable-transitions="false"
          @close="removeTag(tag)">
          {{ tag.value }}
        </el-tag>
        <el-input
          class="input-new-tag"
          v-if="inputVisible"
          v-model="inputValue"
          ref="saveTagInput"
          size="small"
          @keyup.enter.native="handleInputConfirm"
          @blur="handleInputConfirm"
        >
        </el-input>
        <el-button v-else class="button-new-tag" size="small" @click="showInput">添加</el-button>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </el-dialog>
</template>

<script>

import { listParameter, getParameter, delParameter, addParameter, updateParameter } from "@/api/prod/parameter";

export default {
  name: "parameterAssociation",
  data() {
    return {
      visible: false,
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 参数名表格数据
      parameterList: [],
      // 参数值表格数据
      parameterValueList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        needParameterValues: true,
        name: null
      },
      // 表单参数
      form: {
        id: null,
        categoryId: null,
        name: null
      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: "参数名称不能为空", trigger: "blur" },
          { max: 8, message: "参数值长度不超过8字符", trigger: "blur" }
        ]
      },
      categoryId: null,
      inputVisible: false,
      inputValue: ''
    };
  },
  methods: {
    show(categoryId) {
      this.categoryId = categoryId;
      this.getList();
    },
    /** 查询参数名列表 */
    getList() {
      this.loading = true;
      this.queryParams.categoryId = this.categoryId
      listParameter(this.queryParams).then(response => {
        this.parameterList = response.rows;
        this.total = response.total;
        this.loading = false;
        this.visible = true;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null
      };
      this.parameterValueList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加参数名";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id
      getParameter(id).then(response => {
        this.form = response.data;
        this.parameterValueList = response.data.parameterValueList;
        this.open = true;
        this.title = "修改参数名";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.categoryId = this.categoryId;
          this.form.parameterValueList = this.parameterValueList;
          if (this.form.id != null) {
            updateParameter(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addParameter(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id;
      this.$modal.confirm('是否确认删除参数名编号为"' + ids + '"的数据项？').then(function() {
        return delParameter(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },

    concat(parameterValueList){
      let parameterString = "";

      for (let i = 0; i < parameterValueList.length; i++) {
        parameterString += parameterValueList[i].value;

        if(i !== parameterValueList.length - 1){
          parameterString +=  ",";
        }
      }
      return parameterString;
    },
    removeTag(tag) {
      this.parameterValueList.splice(this.parameterValueList.indexOf(tag), 1);
    },
    handleInputConfirm() {
      let inputValue = this.inputValue.trim();
      if (inputValue) {
        if(inputValue.length > 8){
          this.$modal.msgWarning("参数值长度不能超过8个字符!")
          return;
        }

        let existence = this.parameterValueList.some(item => item.value === inputValue);
        if(existence){
          this.$modal.msgWarning("该属性值["+ inputValue +"]已存在!")
          return;
        }
        this.parameterValueList.push({ value: inputValue });
      }
      this.inputVisible = false;
      this.inputValue = '';
    },
    showInput() {
      this.inputVisible = true;
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },
  }
}

</script>
<style>
.el-tag + .el-tag {
  margin-left: 10px;
  margin-top: 10px;
}

.button-new-tag {
  margin-top: 10px;
  margin-left: 10px;
  height: 30px;
  line-height: 28px;
  padding-top: 0;
  padding-bottom: 0;
}

.input-new-tag {
  width: 90px;
  margin-top: 10px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>
